package gapidt.plugin.views;

import gapidt.analysis.APIDesignAnalysisResult;
import gapidt.analysis.DesignProblem;
import gapidt.common.Locale;
import gapidt.plugin.AnalysisResultsManager;
import gapidt.plugin.dialogs.DesignProblemInfoDialog;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.*;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.*;

/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class AnalysisResultsView extends ViewPart {

	public static final String ID = AnalysisResultsView.class.getCanonicalName();

	private TreeViewer viewer;
	
	private Action showJavaElementAction;
	private Action clearResultsAction;
	private Action openDesignProblemDialogAction;
	private Action rerunAnalysisAction;
	
	/**
	 * The constructor.
	 */
	public AnalysisResultsView(){ }

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {

		viewer = new TreeViewer(parent);
		viewer.setContentProvider(new AnalysisResultsContentProvider());
		viewer.setLabelProvider(new AnalysisResultsLabelProvider());
		viewer.setInput(AnalysisResultsManager.getInstance());
		viewer.expandAll();
		
		ColumnViewerToolTipSupport.enableFor(viewer);
		
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
		
	}
	
	private void makeActions() {
		
		rerunAnalysisAction = new Action() {
			public void run() {
				
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				
				if(obj == null)
					AnalysisResultsManager.getInstance().rerunAll();
				else if(obj instanceof APIDesignAnalysisResult)
					AnalysisResultsManager.getInstance().rerunAnalysis((APIDesignAnalysisResult<?>) obj);
				else if(obj instanceof DesignProblem<?>)
					AnalysisResultsManager.getInstance().rerunAnalysis((DesignProblem<?>) obj);

				AnalysisResultsView.this.refresh();
			}
		};
		rerunAnalysisAction.setText(Locale.getMessage("gapidt.plugin.actions.RefreshResults.text"));
		rerunAnalysisAction.setToolTipText(Locale.getMessage("gapidt.plugin.actions.RefreshResults.tooltipText"));
		rerunAnalysisAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(ISharedImages.IMG_TOOL_REDO));
		rerunAnalysisAction.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(ISharedImages.IMG_TOOL_REDO_DISABLED));
		rerunAnalysisAction.setEnabled(false);
		
		clearResultsAction = new Action() {
			public void run() {
				
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				
				if(obj == null)
					AnalysisResultsManager.getInstance().clearResults();
				else if(obj instanceof APIDesignAnalysisResult)
					AnalysisResultsManager.getInstance().removeResult((APIDesignAnalysisResult<?>) obj);
				else if(obj instanceof DesignProblem)
					AnalysisResultsManager.getInstance().removeResult((DesignProblem<?>) obj);

				AnalysisResultsView.this.refresh();
			}
		};
		clearResultsAction.setText(Locale.getMessage("gapidt.plugin.actions.ClearResults.text"));
		clearResultsAction.setToolTipText(Locale.getMessage("gapidt.plugin.actions.ClearResults.tooltipText"));
		clearResultsAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_ELCL_REMOVEALL));
		clearResultsAction.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_ELCL_REMOVEALL_DISABLED));
		clearResultsAction.setEnabled(false);
		
		showJavaElementAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				
				if(obj instanceof DesignProblem)
					AnalysisResultsManager.getInstance().showJavaElement((DesignProblem<?>) obj);
				else if(obj instanceof APIDesignAnalysisResult)
					AnalysisResultsManager.getInstance().showJavaElement((APIDesignAnalysisResult<?>) obj);
			}
		};
		
		openDesignProblemDialogAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				
				if(obj instanceof DesignProblem){
					// Abrir dialogo con información sobre el antipatron
					DesignProblemInfoDialog designProblemDialog = new DesignProblemInfoDialog(AnalysisResultsView.this.getSite().getShell(), (DesignProblem<?>) obj);
					designProblemDialog.open();
				}
			}
		};
		openDesignProblemDialogAction.setText(Locale.getMessage("gapidt.plugin.actions.OpenDesignProblemDialog.text"));
		openDesignProblemDialogAction.setToolTipText(Locale.getMessage("gapidt.plugin.actions.OpenDesignProblemDialog.tooltipText"));
		openDesignProblemDialogAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		
	}
	
	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				
				AnalysisResultsView.this.fillContextMenu(manager, obj);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}
	
	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				openDesignProblemDialogAction.run();
			}
		});
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				showJavaElementAction.run();
			}
		});
	}
	
	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(rerunAnalysisAction);
		manager.add(clearResultsAction);
		manager.add(new Separator());
	}

	private void fillContextMenu(IMenuManager manager, Object selectedElement) {
		if(selectedElement instanceof APIDesignAnalysisResult || selectedElement instanceof DesignProblem){
			manager.add(rerunAnalysisAction);
			manager.add(clearResultsAction);
		}
		if(selectedElement instanceof DesignProblem)
			manager.add(openDesignProblemDialogAction);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(rerunAnalysisAction);
		manager.add(clearResultsAction);
	}
	
	public TreeViewer getViewer() {
		return viewer;
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	/**
	 * Refresh window contents
	 */
	public void refresh() {
		viewer.refresh();
		if(AnalysisResultsManager.getInstance().getResults().size() > 0){
			clearResultsAction.setEnabled(true);
			rerunAnalysisAction.setEnabled(true);
		}else{
			clearResultsAction.setEnabled(false);
			rerunAnalysisAction.setEnabled(false);
		}
	}
}